package com.iflytek.ci.common.service.impl;

import com.iflytek.ci.common.support.XMLNodeParser;

import com.iflytek.ci.common.service.XMLDocumentInterface;

import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA.
 * User: liyasoft
 * Date: 13-1-26
 * Time: 上午9:49
 */
public class XMLDocument implements XMLDocumentInterface{
    @Override
    public void createXML(String fileName){
//        Element root = this.document.createElement("employees");
//        this.document.appendChild(root);
//        Element employee = this.document.createElement("employee");
//        Element name = this.document.createElement("name");
//        name.appendChild(this.document.createTextNode("丁宏亮"));
//        employee.appendChild(name);
//        Element sex = this.document.createElement("sex");
//        sex.appendChild(this.document.createTextNode("m"));
//        employee.appendChild(sex);
//        Element age = this.document.createElement("age");
//        age.appendChild(this.document.createTextNode("30"));
//        employee.appendChild(age);
//        root.appendChild(employee);
//        TransformerFactory tf = TransformerFactory.newInstance();
//        try {
//            Transformer transformer = tf.newTransformer();
//            DOMSource source = new DOMSource(document);
//            transformer.setOutputProperty(OutputKeys.ENCODING, "gb2312");
//            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
//            PrintWriter pw = new PrintWriter(new FileOutputStream(fileName));
//            StreamResult result = new StreamResult(pw);
//            transformer.transform(source, result);
//            System.out.println("生成XML文件成功!");
//        } catch (TransformerConfigurationException e) {
//            System.out.println(e.getMessage());
//        } catch (IllegalArgumentException e) {
//            System.out.println(e.getMessage());
//        } catch (FileNotFoundException e) {
//            System.out.println(e.getMessage());
//        } catch (TransformerException e) {
//            System.out.println(e.getMessage());
//        }
    }
    @Override
    public String parseXML(String fileName,String nodeName){
        XMLNodeParser xmlNodeParser = new XMLNodeParser();
        File file = new File(fileName);
        String version=xmlNodeParser.getTextContent(file,nodeName);
        System.out.println(version);
        String regex="9.9.9-SNAPSHOT";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(version);
        if (m.matches())
        return "true";
        else {
            return "false";
        }
    }
}
